package com.cui;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * @说明 用于显示表格
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
@SuppressWarnings("serial")
public class GridTable_Tag  extends BodyTagSupport{
	
	private HttpServletRequest request ;
	
	@Override
	public void setPageContext(PageContext pageContext) {
		request = (HttpServletRequest)pageContext.getRequest();
		super.setPageContext(pageContext);
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		try {
			StringBuffer results = new StringBuffer("");			
			// 引入文件
			results.append("<link rel='stylesheet' type='text/css' href='" + basePath + "js/style.css' />");
			results.append("<script type='text/javascript' src='" + basePath + "js/jquery.js'></script>");
			results.append("<script type='text/javascript' src='" + basePath + "js/TableView.js'></script>");
			results.append("<script type='text/javascript' src='" + basePath + "js/PagerView.js'></script>");
			results.append("<script type='text/javascript' src='" + basePath + "js/SortView.js'></script>");
			results.append("<script type='text/javascript' src='" + basePath + "js/commonAttribute.js'></script>");
			String[] cn = colunmName.split(",");
			String[] cs = colunmShow.split(",");			
			// 开始绘制表格
			results.append("<script type='text/javascript'>");
			results.append("document.getElementById('table_div').className='tableShow';");
			results.append("var index=1;");
			results.append("var table = new TableView('table_div');");
			results.append("table.header={");
			results.append("index:'序号',");
			for(int i=0;i<cn.length;i++){
				results.append(cn[i] + ":'" + cs[i] + "',");
			}
			results.delete(results.length() - 1, results.length());
			results.append("};");
			results.append("table.dataKey='" + cn[0] + "';");
			String date = "";
			date = null == request.getAttribute("currentJsonDate") ? "" : (String)request.getAttribute("currentJsonDate");
			if(null != date && !"".equals(date))
				date = date.replace("\\", "/"); // 部分数据可能会引起JS解析错误
			results.append("var json='" + date + "';");
			results.append("var objs=eval(json);"); // EVAL函数的使用
			results.append("if(null!=objs){");
			results.append("for(var i=0;i<objs.length;i++){");
			results.append("var u = objs[i];");
			results.append("table.add({");
			results.append("index:index++,");
			for(int i=0;i<cn.length;i++){
				if(i==0){
					results.append(cn[i] + ":u." + cn[i] + ",");
				}else{
					results.append(cn[i] + ":(u." + cn[i] + " + '').substr(0, 20),"); // 这里做了数据截取
				}
			}
			results.delete(results.length() - 1, results.length());
			results.append("});");
			results.append("}");
			results.append("}");
			results.append("table.display.sort=true;");
			// 是否显示所有列
			if(null != showAllColunm && "true".equals(showAllColunm)){
				results.append("table.display.showAllColunm=true;");
			}
			// 是否显示多选框列
			if(null != isShowCheck && "false"==isShowCheck){
				results.append("table.display.multiple=false;");
				results.append("table.display.marker=false;");
			}			
			// 必须显示的进行表格分页，且页面分页不能同时进行
			if("true".equals(isTablePager) && (null == isPager || "false".equals(isPager))){
				results.append("table.display.pager=true;");
				results.append("table.pager.size=pageSize;"); // 分页页数是JS文件里面定义的
			}else{
				results.append("table.display.pager=false;");
			}			
			results.append("table.render();");			
			// 开始显示分页，表格自动分页和显示分页标签不能一同进行
			if(!"false".equals(isPager) && (null == isTablePager || "false".equals(isTablePager))){
				results.append("var pager=new PagerView('pager_div');");				
				results.append("pager.itemCount=" + (null != request.getAttribute("currentPageCount") ? request.getAttribute("currentPageCount").toString() : "0") + ";");
				results.append("pager.size=" + (null != request.getAttribute("currentPageSize") ? request.getAttribute("currentPageSize").toString() : "10") + ";");
				results.append("pager.index=" + (null != request.getAttribute("currentPageIndex") ? request.getAttribute("currentPageIndex").toString() : "0") + ";");
				results.append("pager.render();");
			}
			// 增加Ajax数据变换时的JS方法
			results.append("function setJsonData(data){");
			results.append("if(null!=data&&''!=data){");
			results.append("index=1;");
			results.append("table.clear();"); // 清除原有数据
			results.append("var json=data;");
			results.append("var objs=eval(json);");
			results.append("for(var i=0;i<objs.length;i++){");
			results.append("var u = objs[i];");
			results.append("table.add({");
			results.append("index:index++,");
			for(int i=0;i<cn.length;i++){
				if(i==0){
					results.append(cn[i] + ":u." + cn[i] + ",");
				}else{
					results.append(cn[i] + ":(u." + cn[i] + " + '').substr(0, 20),");
				}
			}
			results.delete(results.length() - 1, results.length());
			results.append("});");
			results.append("table.render();");
			results.append("}");
			results.append("}");
			results.append("}");
			results.append("</script>");			
			results.append("<input type='hidden' id='colunmName' name='colunmName' value='" + colunmName + "'>");
			results.append("<input type='hidden' id='colunmShow' name='colunmShow' value='" + colunmShow + "'>");
			pageContext.getOut().write(results.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return EVAL_BODY_INCLUDE;
	}
	
	protected String colunmName;
	
	protected String colunmShow;
	
	protected String isPager;
	
	protected String showAllColunm;
	
	protected String isTablePager;
	
	protected String currentJsonDate;
	
	protected String currentPageCount;
	
	protected String currentPageSize;
	
	protected String isShowCheck;
	
	protected String currentPageIndex;

	public String getCurrentPageCount() {
		return currentPageCount;
	}

	public void setCurrentPageCount(String currentPageCount) {
		this.currentPageCount = currentPageCount;
	}

	public String getCurrentPageSize() {
		return currentPageSize;
	}

	public void setCurrentPageSize(String currentPageSize) {
		this.currentPageSize = currentPageSize;
	}

	public String getCurrentPageIndex() {
		return currentPageIndex;
	}

	public void setCurrentPageIndex(String currentPageIndex) {
		this.currentPageIndex = currentPageIndex;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getIsPager() {
		return isPager;
	}

	public void setIsPager(String isPager) {
		this.isPager = isPager;
	}

	public String getColunmName() {
		return colunmName;
	}

	public void setColunmName(String colunmName) {
		this.colunmName = colunmName;
	}

	public String getColunmShow() {
		return colunmShow;
	}

	public void setColunmShow(String colunmShow) {
		this.colunmShow = colunmShow;
	}

	public String getCurrentJsonDate() {
		return currentJsonDate;
	}

	public void setCurrentJsonDate(String currentJsonDate) {
		this.currentJsonDate = currentJsonDate;
	}

	public String getIsShowCheck() {
		return isShowCheck;
	}

	public void setIsShowCheck(String isShowCheck) {
		this.isShowCheck = isShowCheck;
	}

	public String getIsTablePager() {
		return isTablePager;
	}

	public void setIsTablePager(String isTablePager) {
		this.isTablePager = isTablePager;
	}

	public String getShowAllColunm() {
		return showAllColunm;
	}

	public void setShowAllColunm(String showAllColunm) {
		this.showAllColunm = showAllColunm;
	}

}
